import os
import random
import shutil
import cv2

# 输入图片文件夹的路径和要抽取的图片数量
def get_img(n):
    input_folder = "/home/ltt/point_track/trackimage1/hml/913_2hml/"
    # input_folder ="/home/ltt/point_track/trackimage1/914_5huaxi/"

    output_folder ="/home/ltt/point_track/trackimage1/hml/evaluate/"
    # output_folder = "/home/ltt/point_track/trackimage1/evaluate/" # 用于存放抽取出的图片
    num_images_to_extract = n  # 想要抽取的图片数量
    # 获取文件夹中所有图片文件的列表
    image_files_ = [f for f in os.listdir(input_folder) if f.endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp'))]
    image_files_.sort()
    image_files = image_files_[0:800]

    # 确保抽取的数量不超过文件夹中的图片数量
    num_images_to_extract = min(num_images_to_extract, len(image_files))
    # 随机选择图片文件
    selected_images = random.sample(image_files, num_images_to_extract)
    # 将选中的图片复制到输出文件夹
    for image_file in selected_images:
        source_path = os.path.join(input_folder, image_file)
        target_path = os.path.join(output_folder, image_file)
        shutil.copyfile(source_path, target_path)
    print(f"{num_images_to_extract} images have been randomly selected and copied to {output_folder}.")
    return 0

# 图片文件夹路径
def evalu(path):
    image_folder = path
    # 获取文件夹中所有图片文件的列表
    image_files = [f for f in os.listdir(image_folder) if f.endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp'))]
    # 排序图片文件列表
    image_files.sort()
    # 初始化当前显示图片的索引
    current_index = 0
    # 创建窗口
    cv2.namedWindow("Image Viewer", cv2.WINDOW_NORMAL)
    while True:
        # 读取当前索引的图片
        image_file = os.path.join(image_folder, image_files[current_index])
        image = cv2.imread(image_file)
        # 显示图片
        cv2.imshow("Image Viewer", image)
        # 等待按键输入，等待时间为100毫秒
        key = cv2.waitKey(1)
        # 如果按下 'q' 键，退出循环
        if key == ord('q'):
            break
        # 如果按下 'n' 键，切换到下一张图片
        elif key == ord('n'):
            current_index = (current_index + 1) % len(image_files)
        # 如果按下 'p' 键，切换到上一张图片
        elif key == ord('p'):
            current_index = (current_index - 1) % len(image_files)
    # 关闭窗口
    cv2.destroyAllWindows()
    e=1
    return e

if __name__ == '__main__':
    # get_img(120)
    # path="/home/ltt/point_track/trackimage1/evaluate/"
    # e=evalu(path)

    path="/home/ltt/point_track/trackimage1/hml/evaluate/"
    e=evalu(path)